package view.controller;

import common.Communication;
import common.Message;
import common.NowUser;
import common.User;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import server.ClientConnServer;
import tools.GetTime;
import view.Emoji;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.concurrent.BrokenBarrierException;

public class ChatController implements Initializable {
    public Label friendname;
    public Label friendid;
    public Label chatfriendid;
    public FlowPane messagelist;
    public TextArea chattext;
    public Button chatbt;
    public ScrollPane scrollpane;
    public MenuButton frequency;
    public ComboBox frequentlybt;
    public ImageView emo;
    public ImageView photo;
    public ImageView file;
    public MenuItem menu1;
    public MenuItem menu2;
    public MenuItem menu3;
    public MenuItem menu4;
    public File localFile;
    public boolean last = true;
    public ArrayList<Communication> messages = new ArrayList<>();
    public ChatController chat;

    public ChatController getChat() {
        return chat;
    }
    public void setChat(ChatController chat) {
        this.chat = chat;
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        friendid.setText(NowUser.friend.getId());
        //刚打开页面聊天内容显示在最底部
        scrollpane.setVvalue(1);
        scrollpane.getVvalue();
        //让发送消息显示在最底部
        scrollpane.vvalueProperty().addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                //判断滑轮是否在最下面
                if (last) {
                    scrollpane.setVvalue(1.0);
                    last = false;
                }
            }
        });
        friendname.setText(NowUser.friend.getName()+"("+NowUser.friend.getId()+")");
        if(NowUser.friend.getHeadimage()==null || NowUser.friend.getHeadimage().equals("") || NowUser.friend.getHeadimage().equals("null")){
            chatfriendid.setText("E:\\untitled\\src\\view\\photo\\默认头像.jpg");
        }else{
            chatfriendid.setText(NowUser.friend.getHeadimage());}
        Message newmsg=new Message();
        newmsg.setType("records");//聊天记录
        Communication comm=new Communication();
        comm.setSender(NowUser.user);
        comm.setReceiver(NowUser.friend);
        newmsg.setCommunication(comm);
        try {
            new ClientConnServer().sendinfo(newmsg);
            NowUser.cyclicBarrier.reset();
            NowUser.cyclicBarrier.await();
            ArrayList<Communication> friendchatlist=NowUser.friendchatlist;
            if(friendchatlist!=null){
                for(int i=0;i<friendchatlist.size();i++)
                {
                    Communication communication=new Communication();
                    communication=friendchatlist.get(i);
                    //System.out.println(communication.getSender().getUserid());
                    if(communication.getSender().getId().equals(NowUser.user.getId())){
                        addMessageBoxr(communication);
                    }else{
                        addMessageBoxl(communication);
                    }
                }}
            System.out.println("meesagelist "+NowUser.user.getId()+NowUser.friend.getId());
            ManageChat.setScrollPaneHashMap(NowUser.user.getId()+NowUser.friend.getId(),scrollpane);
            ManageChat.setMeesagelist(NowUser.user.getId()+NowUser.friend.getId(),messagelist);
            //System.out.println(ManageChat.getMeesagelist("1054014620")+"msglist");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        //设置元素的全高
        VBox.setVgrow(messagelist, Priority.ALWAYS);
        //回车事件，返回文本框内容
        chattext.setOnKeyPressed(new EventHandler<KeyEvent>() {
            @Override
            public void handle(KeyEvent keyEvent) {
                if (keyEvent.getCode() == KeyCode.ENTER) {
                    Message msg = new Message();
                    Communication comm = new Communication();
                    comm.setContent(chattext.getText());
                    comm.setSender(NowUser.user);
                    String time=new GetTime().getTime();//获取时间的工具类
                    comm.setSendDateTime(time);
                    String str=friendname.getText();
                    String friendid="";
                    for(int i=0;i<str.length();i++)
                    {
                        if(i!=0&&str.charAt(i-1)=='('){
                            while(str.charAt(i)!=')'){
                                friendid=friendid+str.charAt(i);
                                i++;
                            }
                        }
                    }
                    //System.out.println(friendid+"ggg");
                    User friend=new User();
                    friend.setId(friendid);
                    comm.setReceiver(friend);
                    comm.setType(0);
                    msg.setType("chatsend");
                    msg.setCommunication(comm);
                    NowUser.chatsend = false;
                    // System.out.println("ggggggg");
                    try {
                        new ClientConnServer().sendinfo(msg);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    NowUser.cyclicBarrier.reset();
                    try {
                        NowUser.cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    addMessageBoxr(comm);
                    chattext.setText("");
                }
            }
        });
    }

    public void tochat(ActionEvent actionEvent) throws IOException,BrokenBarrierException,InterruptedException{
        Message msg = new Message();
        Communication comm = new Communication();
        comm.setContent(chattext.getText());
        comm.setSender(NowUser.user);
        String time=new GetTime().getTime();//获取时间的工具类
        comm.setSendDateTime(time);
        String str=friendname.getText();
        String friendid="";
        for(int i=0;i<str.length();i++)
        {
            if(i!=0&&str.charAt(i-1)=='('){
                while(str.charAt(i)!=')'){
                    friendid=friendid+str.charAt(i);
                    i++;
                }
            }
        }
        User friend=new User();
        friend.setId(friendid);
        comm.setReceiver(friend);
        comm.setType(0);
        msg.setType("chatsend");
        msg.setCommunication(comm);
        NowUser.chatsend = false;
        //System.out.println("ggggggg");
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        addMessageBoxr(comm);
        chattext.setText("");
    }

    //设置发送到消息文字框(右气泡
    public void addMessageBoxr(Communication communication) {
        URL url = null;
        try {
            if(communication.getSender().getHeadimage()==null || communication.getSender().getHeadimage().equals("") || communication.getSender().getHeadimage().equals("null")){
                url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
            }else {
                url = new URL("file:" + communication.getSender().getHeadimage());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Image image = new Image(String.valueOf(url));
        ImageView imageView = new ImageView(image);
        imageView.setFitHeight(50);
        imageView.setFitWidth(50);
        Circle circle = new Circle();
        circle.setCenterX(25);
        circle.setCenterY(25);
        circle.setRadius(25);
        imageView.setClip(circle);
        Label messageBubble=new Label();
        ImageView imgv = new ImageView();
        //显示时间
//        Label timeBubble = new Label(communication.getSendDateTime());
//        timeBubble.setWrapText(true);
//        timeBubble.setFont(Font.font(16));
//        timeBubble.setMaxWidth(100);
//        timeBubble.setMinWidth(100);
        //timeBubble.setStyle("-fx-background-color: transparent;");
        //System.out.println(timeBubble+"高数");
        //VBox.setMargin(timeBubble, new Insets(18, 10, 10, 10));
        if(communication.getType()==0) {//发送文字
            messageBubble = new Label(communication.getContent());
            //换行
            messageBubble.setWrapText(true);
            messageBubble.setMinWidth(25);
            messageBubble.setMaxWidth(220);
            messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            messageBubble.setPadding(new Insets(6));
            messageBubble.setFont(new Font(14));
            HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));
            double[] points;
            points = new double[]{
                    0.0, 0.0,
                    0.0, 10.0,
                    10.0, 5.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            //设置位置
            messageBox.setPrefWidth(585);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
            messageBox.getChildren().addAll(messageBubble, triangle, imageView);
            messageBox.setAlignment(Pos.TOP_RIGHT);
            last = scrollpane.getVvalue() == 1.0;
            messagelist.getChildren().add(messageBox);
        }else if(communication.getType()==1){//发送图片
            Image imgpath = new Image("file:"+communication.getContent());
            imgv = new ImageView(imgpath);
            //换行
            imgv.setFitHeight(220);
            imgv.setFitWidth(220);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));
            double[] points;
            points = new double[]{
                    0.0, 0.0,
                    0.0, 10.0,
                    10.0, 5.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(585);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
            messageBox.getChildren().addAll(imgv, triangle, imageView);
            messageBox.setAlignment(Pos.TOP_RIGHT);
            last = scrollpane.getVvalue() == 1.0;
            messagelist.getChildren().add(messageBox);
        }else if(communication.getType()==2){//发送文件
            Image imgpath = new Image("file:"+"E:\\untitled\\src\\view\\photo\\标\\上传文件.png");
            imgv = new ImageView(imgpath);
            //换行
            imgv.setFitHeight(220);
            imgv.setFitWidth(220);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            imgv.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {
                    File opfile=new File(communication.getContent());
                    if(opfile.exists()){
                        Desktop desktop = Desktop.getDesktop();
                        try {
                            desktop.open(opfile);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));
            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));
            double[] points;
            points = new double[]{
                    0.0, 0.0,
                    0.0, 10.0,
                    10.0, 5.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(585);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
            messageBox.getChildren().addAll(imgv, triangle, imageView);
            messageBox.setAlignment(Pos.TOP_RIGHT);
            last = scrollpane.getVvalue() == 1.0;
            messagelist.getChildren().add(messageBox);
        }else if(communication.getType()==4){//发送表情
            Image imgpath = new Image("file:"+communication.getContent());
            imgv = new ImageView(imgpath);
            //换行
            imgv.setFitHeight(35);
            imgv.setFitWidth(35);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));
            double[] points;
            points = new double[]{
                    0.0, 0.0,
                    0.0, 10.0,
                    10.0, 5.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(585);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
            messageBox.getChildren().addAll(imgv, triangle, imageView);
            messageBox.setAlignment(Pos.TOP_RIGHT);
            last = scrollpane.getVvalue() == 1.0;
            messagelist.getChildren().add(messageBox);
        }
    }

    //左气泡
    public void addMessageBoxl(Communication communication) {
        //System.out.println(chatfriendid.getText());
        URL url = null;
        try {
            if(communication.getSender().getHeadimage()==null || communication.getSender().getHeadimage().equals("") || communication.getSender().getHeadimage().equals("null")){
                url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
            }else {
                url = new URL("file:" + communication.getSender().getHeadimage());
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        Image image = new Image(String.valueOf(url));
        ImageView imageView = new ImageView(image);
        imageView.setFitHeight(50);
        imageView.setFitWidth(50);
        Circle circle = new Circle();
        circle.setCenterX(25);
        circle.setCenterY(25);
        circle.setRadius(25);
        imageView.setClip(circle);
        Label messageBubble = new Label();
        ImageView imgv = new ImageView();
        if(communication.getType()==0) {
            messageBubble = new Label(communication.getContent());
            messageBubble.setWrapText(true);
            messageBubble.setMaxWidth(220);
            messageBubble.setMinWidth(25);
            messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            messageBubble.setPadding(new Insets(6));
            messageBubble.setFont(new Font(14));
            HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));
            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));
            double[] points;
            points = new double[]{
                    0.0, 5.0,
                    10.0, 0.0,
                    10.0, 10.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(366);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
            messageBox.getChildren().addAll(imageView, triangle, messageBubble);
            last = scrollpane.getVvalue() == 1.0;
            messagelist.getChildren().add(messageBox);
        }else if(communication.getType()==1) {//发送图片
            Image imgpath = new Image("file:" + communication.getContent());
            imgv = new ImageView(imgpath);
            imgv.setFitHeight(220);
            imgv.setFitWidth(220);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));
            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));
            double[] points;
            points = new double[]{
                    0.0, 5.0,
                    10.0, 0.0,
                    10.0, 10.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(366);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
            messageBox.getChildren().addAll(imageView, triangle, imgv);
            last = scrollpane.getVvalue() == 1.0;
            messagelist.getChildren().add(messageBox);
        }else if(communication.getType()==4){//发送表情
            Image imgpath = new Image("file:" + communication.getContent());
            imgv = new ImageView(imgpath);
            //System.out.println(communication.getContent()+"怕怕怕怕怕");
            //换行
            imgv.setFitHeight(35);
            imgv.setFitWidth(35);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));
//            imgv.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
//                @Override
//                public void handle(MouseEvent mouseEvent) {
//                    File opfile=new File(communication.getContent());
//                    if(opfile.exists()){
//                        Desktop desktop = Desktop.getDesktop();
//                        try {
//                            desktop.open(opfile);
//                        } catch (IOException e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            });
//            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));
            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));
            double[] points;
            points = new double[]{
                    0.0, 5.0,
                    10.0, 0.0,
                    10.0, 10.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(366);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
            messageBox.getChildren().addAll(imageView, triangle, imgv);
            last = scrollpane.getVvalue() == 1.0;
            messagelist.getChildren().add(messageBox);
        }else if(communication.getType()==2) {//发送文件
            Image imgpath = new Image("file:" + "E:\\untitled\\src\\view\\photo\\标\\上传文件.png");
            imgv = new ImageView(imgpath);
            imgv.setFitHeight(220);
            imgv.setFitWidth(220);
            imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
            //imgv.setPadding(new Insets(6));
            //imgv.setFont(new Font(14));
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));
            imgv.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent mouseEvent) {
                    File opfile=new File(communication.getContent());
                    if(opfile.exists()){
                        Desktop desktop = Desktop.getDesktop();
                        try {
                            desktop.open(opfile);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

            Label timeBubble = new Label(communication.getSendDateTime());
            timeBubble.setWrapText(true);
            timeBubble.setMaxWidth(180);
            timeBubble.setMinWidth(25);
            timeBubble.setStyle("-fx-background-color: transparent;");
            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

            double[] points;
            points = new double[]{
                    0.0, 5.0,
                    10.0, 0.0,
                    10.0, 10.0
            };
            Polygon triangle = new Polygon(points);
            //设置颜色
            triangle.setFill(Color.rgb(179, 231, 244));
            HBox messageBox = new HBox();
            messageBox.setPrefWidth(366);
            //设置空隙
            messageBox.setPadding(new Insets(10, 5, 10, 5));
            HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
            messageBox.getChildren().addAll(imageView, triangle, imgv);
            last = scrollpane.getVvalue() == 1.0;
            messagelist.getChildren().add(messageBox);
        }
    }

    //发表情
    public void toemoji(MouseEvent mouseEvent) throws IOException, BrokenBarrierException, InterruptedException {
        User user = new User();
        user.setId(friendid.getText());
        NowUser.friend = user;
        Stage stage = (Stage) emo.getScene().getWindow();
        Stage newstage=new Stage();
        Emoji rj=new Emoji();
        try {
            rj.start(newstage);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //发图片
    public void toimg(MouseEvent mouseEvent) throws IOException, BrokenBarrierException, InterruptedException {
        FileChooser fileChooser = new FileChooser(); //文件选择窗口
        fileChooser.setTitle("Open Resource File");  //窗口名
        fileChooser.setInitialDirectory(new File("C:\\Users")); //设置默认打开的文件路径
        File file = fileChooser.showOpenDialog(new Stage());//显示屏幕选择窗口
        // String filePath = file.getCanonicalPath()+"/fileSource"; //指定路径
        String str =null;
        if(file != null) {
            str = String.valueOf(file.getAbsolutePath());  // 方法返回选择文件的相对路径
            String url = "file:" + str;
            NowUser.user.setHeadimage(url);
            //读取本地文件
            localFile = new File(str);
            //emoji.setImage(new Image(url,127,108,true,true,true));
        }
        String friendnameText=friendname.getText();
        String friendid="";
        for(int i=0;i<friendnameText.length();i++)
        {
            if(i!=0&&friendnameText.charAt(i-1)=='('){
                while(friendnameText.charAt(i)!=')'){
                    friendid=friendid+friendnameText.charAt(i);
                    i++;
                }
            }
        }
        Message msg = new Message();
        User friend=new User();
        Communication comm = new Communication();
        friend.setId(friendid);
        comm.setReceiver(friend);
        comm.setSendDateTime(new GetTime().getTime());
        comm.setSender(NowUser.user);
        comm.setType(1);
        msg.setHeadimage(localFile);
        msg.setType("sendphoto");
        msg.setCommunication(comm);
        NowUser.sendphoto = false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        addMessageBoxr(NowUser.communication);
        chattext.setText("");
    }

    public void openFile(MouseEvent mouseEvent) throws IOException, BrokenBarrierException, InterruptedException {
        FileChooser fileChooser = new FileChooser(); //文件选择窗口
        fileChooser.setTitle("Open Resource File");  //窗口名
        fileChooser.setInitialDirectory(new File("C:\\Users")); //设置默认打开的文件路径
        File file = fileChooser.showOpenDialog(new Stage());//显示屏幕选择窗口
        // String filePath = file.getCanonicalPath()+"/fileSource"; //指定路径
        String str =null;
        if(file != null) {
            str = String.valueOf(file.getAbsolutePath());  // 方法返回选择文件的相对路径
            String url = "file:" + str;
            NowUser.user.setHeadimage(url);
            //读取本地文件
            localFile = new File(str);
            //emoji.setImage(new Image(url,127,108,true,true,true));
        }
        String friendnameText=friendname.getText();
        String friendid="";
        for(int i=0;i<friendnameText.length();i++)
        {
            if(i!=0&&friendnameText.charAt(i-1)=='('){
                while(friendnameText.charAt(i)!=')'){
                    friendid=friendid+friendnameText.charAt(i);
                    i++;
                }
            }
        }
        Message msg = new Message();
        User friend=new User();
        Communication comm = new Communication();
        friend.setId(friendid);
        comm.setReceiver(friend);
        comm.setSendDateTime(new GetTime().getTime());
        comm.setSender(NowUser.user);
        comm.setType(2);
        msg.setHeadimage(localFile);
        msg.setType("sendfile");
        msg.setCommunication(comm);
        NowUser.sendphoto = false;
        new ClientConnServer().sendinfo(msg);
        NowUser.cyclicBarrier.reset();
        NowUser.cyclicBarrier.await();
        addMessageBoxr(NowUser.communication);
        chattext.setText("");
    }

    public void on1(ActionEvent actionEvent) {
        chattext.appendText(menu1.getText());
    }

    public void on2(ActionEvent actionEvent) {
        chattext.appendText(menu2.getText());
    }

    public void on3(ActionEvent actionEvent) {
        chattext.appendText(menu3.getText());
    }

    public void on4(ActionEvent actionEvent) {
        chattext.appendText(menu4.getText());
    }
}
